package com.gskj.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.entity.EduCourse;
import com.gskj.eduservice.entity.EduTeacher;
import com.gskj.eduservice.service.IEduCourseService;
import com.gskj.eduservice.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :CodingFarmer_码农  Connor(康纳)_郭
 * @date : 15:58 20.5.22
 * @Company : http://www.gskj.com
 * @Version : 1.0
 * <p>
 * <p>
 * 讲师 前台控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher/front")
public class TeacherFrontController {

    @Autowired
    private IEduTeacherService eduTeacherService;

    @Autowired
    private IEduCourseService eduCourseService;


    /***
     *  前端查询讲师分页集合
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/get/teacher/front/pagelist/{page}/{size}")
    public ResultJson getTeacherPage(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size) {
        // 分页对象
        Page<EduTeacher> eduTeacherPage = new Page<>(page,size);

        Map<String,Object> map = eduTeacherService.getTeacherFrontPageList(eduTeacherPage);
        return ResultJson.success().data(map);
    }

    /***
     *   根据id 查询 讲师信息。并显示 4个课程信息列表
     * @return
     */
    @GetMapping("/get/teacher/info/course/{teacherId}")
    public ResultJson getTeacherInfoByIdAndCourseList(
            @PathVariable("teacherId")String tId){

        // 根据id 查询讲师的信息
        EduTeacher teacher = eduTeacherService.getById(tId);



        // 根据讲师id 查询他所属的课程信息。
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", tId);
        Page<EduCourse> eduCoursePage = new Page<>(1, 4);
        // 分页查询四条课程
        eduCourseService.page(eduCoursePage, wrapper);
        List<EduCourse> courseList = eduCoursePage.getRecords();

        // 如果该讲师没有所属课程。则默认查询其他随机4条课程
        if(courseList.size()==0){
            eduCourseService.page(eduCoursePage, null);
            courseList =  eduCoursePage.getRecords();
        }

        //封装map 返回前台
        Map<String,Object> map = new HashMap<>();
        map.put("teacher", teacher);
        map.put("courseList", courseList);

        return ResultJson.success().data(map);
    }
}
